import '@babel/polyfill' // (一定要在最上面，第一行)
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import './public-path'
import actions from './cros/actions'
import Vue from 'vue'
import VueRouter from 'vue-router'
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import { dictTranslation } from '@/utils/index'
// eslint-disable-next-line no-unused-vars
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import locale from 'element-ui/lib/locale/lang/en' // lang i18n

import '@/styles/index.scss' // global css
// import service from './utils/request'
import { setServicePath } from '@/api/baseUrlConfig'
import App from './App'
import store from './store'
import { constantRoutes } from './router'
import Pagination from '@/components/Pagination/index.vue'
import CritComponents from 'crit-components'
import 'crit-components/style/critUi.css'
import '@/icons' // icon
import '@/permission' // permission control
import * as filters from '@/utils/filters'

// 引入MD编辑器
// import './plugins/MarkdownEditor'
// set ElementUI lang to EN
// Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI, { size: 'small' })
Vue.use(CritComponents)
Vue.use(VXETable)

// 全局挂载组件
Vue.component('Pagination', Pagination) // 分页
Vue.config.productionTip = false
Vue.prototype.$dictTranslation = dictTranslation
// 挂载全局过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
let router = null
let instance = null

function render (props = {}) {
  if (!window.__POWERED_BY_QIANKUN__) {
    return Message({
      message: '请输入正确的服务地址',
      type: 'error',
      duration: 5000
    })
  }
  const { container, routerBase, mode, requestBase, modeServicePath, filePreview } = props
  if (props) actions.setActions(props)
  setServicePath(modeServicePath)
  Vue.prototype.$filePreview = filePreview
  // console.log(servicePath, modeServicePath, '接口前缀')
  console.log(window.__POWERED_BY_QIANKUN__, routerBase, process.env.VUE_APP_PUBLIC_PATH, requestBase, 'routerBase')
  // initRequest({ requestBase })
  router = new VueRouter({
    mode: mode || 'hash',
    routes: constantRoutes,
    base: window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.VUE_APP_PUBLIC_PATH
  })
  instance = new Vue({
    router,
    store,
    render: (h) => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app')
}

if (!window.__POWERED_BY_QIANKUN__) {
  render()
}

export async function bootstrap () {
  console.log('vue app bootstraped')
}

export async function mount (props) {
  console.log(window.localStorage.getItem('organId'), '本地')

  render(props)
  const name = process.env.VUE_APP_NAME
  console.log('name', name)

  const childBox = document.getElementById(`__qiankun_microapp_wrapper_for_${name}__`)
  if (childBox) childBox.style.height = '100%'
  const childBox1 = document.getElementById('appContainer')
  if (childBox1) childBox1.style.height = '100%'
}

export async function unmount () {
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
  router = null
  const childBox1 = document.getElementById('appContainer')
  childBox1.style.height = '0'
}
export async function update (props) {
  console.log('update props', props)
}
